package com.eslserver.action.group;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.eslserver.core.ESLServerPageDispatcher;
import com.eslserver.core.aware.GroupAware;
import com.eslserver.dao.DAOException;
import com.eslserver.dao.ESLDAO;
import com.eslserver.dao.ESLServerDAOFactory;
import com.eslserver.dao.GroupDAO;
import com.eslserver.to.ESL;
import com.eslserver.to.Group;

@SuppressWarnings("serial")
public class AddGroup extends HttpServlet implements GroupAware,
		ESLServerPageDispatcher {

	public static final Logger LOG = LoggerFactory.getLogger(AddGroup.class);

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			Integer groupID = Integer.valueOf(req.getParameter(PARAM_GROUP_ID));

			GroupDAO groupDAO = ESLServerDAOFactory.getDAOFactory(
					ESLServerDAOFactory.ESLSERVER_DAOFACTORY).getGroupDAO();

			ESLDAO eslDAO = ESLServerDAOFactory.getDAOFactory(
					ESLServerDAOFactory.ESLSERVER_DAOFACTORY).getESLDAO();
			Collection<ESL> oldESLs = null;
			String[] selectedESLIDs = null;
			Collection<ESL> newESLs = new ArrayList<ESL>();

			Group group = null;
			Date now = new Date();
			if (groupID.equals(0)) {
				group = Group.createGroup();
				group.setCreateDate(now);
				group.setModifyDate(now);
				group.setDelete(false);
			} else {
				group = groupDAO.loadGroup(groupID);
				oldESLs = groupDAO.loadGroup(groupID).getEsls();

				for (ESL esl : oldESLs) {
					esl.setGroup(null);
					eslDAO.updateESL(esl);
				}
			}

			if (req.getParameter(PARAM_GROUP_NAME) != null
					&& req.getParameter(PARAM_GROUP_NAME).length() > 0) {
				group.setName(req.getParameter(PARAM_GROUP_NAME));
			}
			// if (req.getParameterValues(PARAM_GROUP_SELECTED_ESL) != null
			// && req.getParameterValues(PARAM_GROUP_SELECTED_ESL).length > 0) {
			// selectedESLIDs = req
			// .getParameterValues(PARAM_GROUP_SELECTED_ESL);
			// for (String eslID : selectedESLIDs) {
			// newESLs.add(eslDAO.loadESL(Integer.parseInt(eslID)));
			// }
			// }
			if (req.getParameterValues(PARAM_GROUP_SELECTED_ESL) != null
					&& req.getParameterValues(PARAM_GROUP_SELECTED_ESL).length > 0) {
				selectedESLIDs = req
						.getParameterValues(PARAM_GROUP_SELECTED_ESL);
				for (String eslID : selectedESLIDs) {
					newESLs.add(eslDAO.loadESL(Integer.parseInt(eslID)));
				}
			}

			if (req.getParameter(PARAM_GROUP_DESCRIPTION) != null) {
				group.setDescription(req.getParameter(PARAM_GROUP_DESCRIPTION));
			}

			if (groupID.equals(0)) {
				groupDAO.addGroup(group);
				groupID = group.getGroupID();
			} else {
				groupDAO.updateGroup(group);
			}

			for (ESL esl : newESLs) {
				esl.setGroup(group);
				eslDAO.updateESL(esl);
			}

			req.setAttribute(ATTRIBUTE_GROUP_ID, groupID);

			RequestDispatcher dispatcher = req.getRequestDispatcher(GROUP_INFO);
			dispatcher.forward(req, resp);

		} catch (DAOException e) {
			LOG.error("Error adding Group(s).", e);
		}
	}

}
